package com.smartlifev30.baiwei.view.banner;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;

/**
 * Created by tys on 2018/8/16.
 */

class BannerCore extends ViewGroup {
    int childWidth;
    private int childHeight;
    private int mMaximumVelocity;
    private int mMinimumVelocity;
    private VelocityTracker mVelocityTracker;
    private boolean isClick;
    private float startX;
    int childCount;
    int last;
    int current;
    /**
     * 按下时停止滚动播放
     */
    volatile boolean isAutoScan = true;
    private BannerView.BannerClickListener clickListen;

    public BannerCore(Context context) {
        this(context, null);
    }

    public BannerCore(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BannerCore(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        final ViewConfiguration configuration = ViewConfiguration.get(getContext());
        mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
        mMinimumVelocity = (int) (400 * getResources().getDisplayMetrics().density);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        if (changed) {
            for (int i = 0; i < childCount; i++) {
                View view = getChildAt(i);
                int indexStart = i * childWidth;
                view.layout(indexStart, 0, indexStart + childWidth, childHeight);
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        childCount = getChildCount();
        if (childCount > 0) {
            measureChildren(widthMeasureSpec, heightMeasureSpec);
            View childView = getChildAt(0);
            childWidth = childView.getMeasuredWidth();
            childHeight = childView.getMeasuredHeight();
            setMeasuredDimension(childWidth * childCount, childHeight);
        } else {
            setMeasuredDimension(0, 0);
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }


    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        }
        mVelocityTracker.addMovement(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = event.getX();
                //按下时不要滚动播放
                isAutoScan = false;
                isClick = true;
                break;
            case MotionEvent.ACTION_MOVE:
                float moveX = event.getX();
                float difX = startX - moveX;
                scrollBy((int) difX, 0);
                startX = moveX;
                if (Math.abs(difX) > 3) {
                    isClick = false;
                }
                break;
            case MotionEvent.ACTION_UP:
                int scrollX = getScrollX();
                current = (scrollX + childWidth / 2) / childWidth;
                if (isClick) {
                    if (clickListen != null) {
                        clickListen.onBannerClick(current);
                    }
                } else {
                    final VelocityTracker velocityTracker = mVelocityTracker;
                    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
                    int initialVelocity = (int) velocityTracker.getXVelocity();
                    if (Math.abs(initialVelocity) > mMinimumVelocity) {
                        current = initialVelocity > 0 ? --current : ++current;
                    }
                    if (current >= childCount) {
                        current = 0;
                    } else if (current < 0) {
                        current = childCount - 1;
                    }
                    scrollTo(current);
                }
                isAutoScan = true;
                break;
            default:
        }
        return true;
    }

    void scrollTo(int current) {
        scrollTo(current * childWidth, 0);
        currentBannerImagePosition(last, current);
        last = current;
    }

    /**
     * 滑动选中某个view时触发
     *
     * @param last    上一次被选中的位置
     * @param current 被选中的view的位置
     */
    protected void currentBannerImagePosition(int last, int current) {
        Log.i("0-0-", "last:" + last + " current:" + current);
    }

    public void setBannerClickListen(BannerView.BannerClickListener l) {
        this.clickListen = l;
    }


}
